﻿using System;
using System.IO;
using System.Windows.Controls;
using DyUi.Base.Interfaces.SubSystems;
using DyUi.Interfaces;
using Constants = DyUi.Base.Constants.SubSystems.AppletSubSystem;

namespace DyUi.Core.SubSystems
{
    public class AppletSubSystem : DyUiControllerSubSystem, IAppletSubSystem
    {
        public override void Prepare()
        {
            base.Prepare();

            Controller.MessageSubSystem.SubscribeToEvent(this, (m, a) => m == Constants.Message_Request_AppletDirs_Update, OnRequestAppletDirs);
        }

        void OnRequestAppletDirs(Guid id, String message, Object args)
        {
            Controller.MessageSubSystem.PublishEvent(this, Constants.Message_Notify_AppletDirs_Update, "Ttest");
        }

        public void CreateAppletInstance(Guid id, String name, Object arg)
        {
            var path = Path.Combine(@"D:\Development\Projects\DyUi\DyUi.Samples", arg + ".dyui");
            var control = Controller.ControlSubSystem.CreateControl(path);
            control.Prepare();

            var surface = ((UserControl)Controller.ControlSubSystem.RootControl).FindChild<UserControl>("AppletSurface");

            if (surface != null && control != null)
            {
                surface.Content = control;
            }
        }
    }
}
